﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YZQ_DotNetArx.CAD;

namespace YZQ_DotNetArx.Test
{
    public static class 判断多段线顺时针或者逆时针
    {

        [CommandMethod("CCSS")]
        public static void JudgeClockwise()
        {
            PromptEntityResult per = Env.Editor.GetEntity("\n选择多段线");
            if (per.Status != PromptStatus.OK)
                return;
            bool sn = false;
            using (Transaction trans = Env.Database.TransactionManager.StartTransaction())
            {
                Polyline pline = per.ObjectId.GetObject(OpenMode.ForRead) as Polyline;
                sn=pline.IsCounterClockwise();
            }
            Env.Print1(sn);
        }
        public static bool IsCounterClockwise(this Polyline pline)
        {
            double area = 0.0;
            for (int i = 0; i < pline.NumberOfVertices - 1; i++)
            {
                Point2d pt1 = pline.GetPoint2dAt(i);
                Point2d pt2 = pline.GetPoint2dAt(i + 1);
                area += pt1.X * pt2.Y - pt2.X * pt1.Y;
            }
            return area < 0;
        }
    }

}
